package com.class06.config;

import com.alibaba.druid.pool.DruidDataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
// 0、准备配置类
// 标识为配置类
@Configuration
// 扫描包路径
@ComponentScan("com.class06")
// 指定外部配置文件路径
@PropertySource("classpath:jdbc.properties")
public class MvcConfig {

//    1、从外部配置文件中读取数据
    @Value("${class06.url}")
    private String url;
    @Value("${class06.user}")
    private String username;
    @Value("${class06.pwd}")
    private String password;
    @Value("${class06.driver}")
    private String driver;


//    2、准备数据源，并且注入到IOC容器中
    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driver);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return  dataSource;
    }


//    3、准备JdbcTemplate，并且注入到IOC容器中,利用参数传递数据源

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

}
